<template>
  <div class="header">
    <van-form @submit="onSubmit">
      <van-cell-group inset>
        <van-field v-model="addr" name="addr" label="地址" placeholder="请输入用户地址"
          :rules="[{ required: true, message: '请填写用户名' }]" />
      </van-cell-group>
      <div style="margin: 16px;">
        <van-button round block type="primary" native-type="submit">
          分发选票
        </van-button>
      </div>
    </van-form>

  </div>
</template>

<script>
import { ref } from 'vue';
import useWeb3 from '../util.js';
import { showSuccessToast } from 'vant';

const { ballotSm, web3 } = useWeb3();

const addr = ref('');

const onSubmit = async (values) => {
  console.log('submit', values);
  // 获取当前账户
  const accounts = await web3.eth.getAccounts();
  const defaultAccount = accounts[0];

  const receipt = await ballotSm.methods.giveRightToVote(values.addr).send({
    from: defaultAccount,
    gas: 1000000,
    gasPrice: '10000000000',
  })
  console.log("receipt", receipt);
  showSuccessToast('分发选票成功');
};

export default {
  name: 'SendView',
  props: {

  },
  setup() {
    return {
      addr,
      onSubmit
    }
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
.header {
  margin: 40px 0 0;
}

</style>